package algorithm;

import java.util.*;

/**
 * 将一个给定字符串根据给定的行数，以从上往下、从左到右进行 Z 字形排列。
 * 示例 1:
 * 输入: s = "LEETCODEISHIRING", numRows = 3
 * 输出: "LCIRETOESIIGEDHN"
 *
 * 示例 2：
 * 输入: s = "LEETCODEISHIRING", numRows = 4
 * 输出: "LDREOEIIECIHNTSG"
 */
public class StringZ {
    public static void main(String[] args) {
        String s = "LEETCODEISHIRING";
        int num = 4;
        convert_new(s,num);
        //convert(s,num);
    }

    public static void convert_new(String s, int numRows){
        if(s.length() <= 1) return;
        char[] chars = s.toCharArray();
        String[] arr = new String[numRows];
        Arrays.fill(arr, "");
        int period = numRows*2-2;//总结得出2n-2为一个周期
        for(int i=0;i<chars.length-1;i++) {
            int mod = i % period;
            if(mod < numRows){
                arr[mod] += chars[i];
            } else {
                arr[period-mod] += chars[i];
            }
        }
        //横向结果
        System.out.println(Arrays.toString(arr));
    }

    public static void convert(String s, int numRows){
        char[] chars = s.toCharArray();
        System.out.println("数据长度："+chars.length);
        List<String> list = new ArrayList<>();
        Map<Integer,List<String>> map = new HashMap<>();
        int tmpNode = numRows-2;
        int j = 1;//计步
        int n = 1;//计步连接点的长度
        int m = 0;//map的key
        for(int i=0;i<chars.length-1;i++){
            System.out.println(">"+chars[i]);
            if(j>numRows){
                list = new ArrayList<>();
                list.add(String.valueOf(chars[i]));
                map.put(m,list);
                m++;
                if(n >= tmpNode){
                    n = 0;
                    j = 0;
                    list = new ArrayList<>();
                }
                n++;
            } else {
                list.add(String.valueOf(chars[i]));
                if(j+1>numRows){
                    map.put(m,list);
                    m++;
                }
            }
            j++;
        }
        map.put(m,list);
        System.out.println(map.toString());
    }

}
